package com.ssyu.dome.common;

import java.util.List;

/**
 * 分页实体
 * 
 * @author Administrator
 * @param <T>
 */
public class PageModel<T> {

    public static final int DEFAULT_PAGE_NO = 1;
    public static final int DEFAULT_PAGE_SIZE = 10;

    /**
     * 结果集
     */
    private List<T> list;

    /**
     * 总记录数
     */
    private int totalRecords;

    /**
     * 每页显示数
     */
    private int pageSize;

    /**
     * 当前是第几页
     */
    private int pageNo;

    public PageModel() {
        super();
    }

    public PageModel(List<T> list, int totalRecords, int pageSize, int pageNo) {
        super();
        this.list = list;
        this.totalRecords = totalRecords;
        this.pageSize = pageSize;
        this.pageNo = pageNo;
    }

    /**
     * 总页数
     * 
     * @return
     */
    public int getTotalPages() {
        return (int) Math.ceil((double) totalRecords / pageSize);
    }

    /**
     * 首页
     * 
     * @return
     */
    public int getFirst() {
        return 1;
    }

    /**
     * 尾页
     * 
     * @return
     */
    public int getLast() {
        return getTotalPages();
    }

    /**
     * 上一页
     * 
     * @return
     */
    public int getPre() {
        if (pageNo == 1) {
            return getFirst();
        }
        return pageNo - 1;
    }

    /**
     * 下一页
     * 
     * @return
     */
    public int getNext() {
        if (pageNo == getTotalPages()) {
            return getTotalPages();
        }
        return pageNo + 1;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public int getTotalRecords() {
        return totalRecords;
    }

    public void setTotalRecords(int totalRecords) {
        this.totalRecords = totalRecords;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getPageNo() {
        return pageNo;
    }

    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

    @Override
    public String toString() {
        return "PageModel [list=" + list + ", totalRecords=" + totalRecords + ", pageSize=" + pageSize + ", pageNo=" + pageNo + "]";
    }

}
